/**
 * 
 */

package com.chengyu.eyc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.chengyu.eyc.constant.EycConstants;
import com.chengyu.eyc.exception.GenericException;
import com.chengyu.eyc.utils.JWTUtil;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kevin
 *
 */
@Slf4j
@Component
public class EycWechatHandlerInterceptor implements HandlerInterceptor {

    /**
     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token=request.getHeader(EycConstants.TOKEN);
        log.info("token:{}", token);
        if(StrUtil.isBlank(token)) {
            throw new GenericException("用户未登录,请先登录");
        }
        if(!JWTUtil.validateJWT(token)) {
            throw new GenericException("非法用户请求");
        }
        return Boolean.TRUE;
    }

}
